﻿namespace ForestryIS.DBGenerator
{
    using System;
    using System.Reflection;

    using Model.Entities;

    using NHibernate;
    using NHibernate.Cfg;
    using NHibernate.Tool.hbm2ddl;

    internal class Program
    {
        static ISessionFactory factory;

        static ISession OpenSession()
        {
            if ( factory == null )
            {
                var c = new Configuration();
                c.Configure();
                c.AddAssembly( Assembly.GetCallingAssembly() );
                factory = c.BuildSessionFactory();
            }
            return factory.OpenSession();
        }

        private static void Test()
        {
            using ( ISession session = OpenSession() )
            {
                using ( ITransaction transaction = session.BeginTransaction() )
                {
                    var tobin = new ProtectionCategory
                                {
                                        Code = "0100010",
                                        Value = "Test Value"
                                };

                    session.Save( tobin );

                    transaction.Commit();
                }
                Console.WriteLine( "Saved Tobin to the database" );
                Console.ReadLine();
            }
        }

        private static void Main( string[] args )
        {
            //Test();
            //return;

            var nhConfig = new Configuration();
            nhConfig.Configure();
            nhConfig.AddAssembly( Assembly.GetExecutingAssembly() );

            // Generate db
            var schemaExport = new SchemaExport( nhConfig );
            schemaExport.Create( false, true );

            // Generate sql script
            schemaExport
                    .SetOutputFile( @"db.sql" )
                    .Execute( false, false, false );

            //Console.ReadLine();
        }
    }
}
